<?php
/**
 * [商网通 System] Copyright swtshop.cn
 * QQ: 1170787711
 * Author: gj.name
 * This is NOT a freeware
*/
defined('IN_UQ') or exit('Access Denied');
class category_attr_uqcms extends control
{
    function __construct()
    {
        $this->aid = $_SESSION['admin']['aid'];
        parent::__construct();
    }
    public function index()
    {
        $id = isset($_GET['id']) ? $_GET['id'] : '0';
        $page = isset($_GET['page']) ? $_GET['page'] : '1';
        $page_size = isset($_GET['page_size']) ? $_GET['page_size'] : '20';
        $cats_query = $this->db->query("select * from " . table("category"));
        while ($cats_row = $this->db->fetch_array($cats_query)) {
            $gcats[$cats_row['cid']] = $cats_row['name'];
        }
        $query = $this->db->get_all("select * from " . table('category_attr') . " where pid = 0 order by id desc limit " . ($page - 1) * $page_size . "," . $page_size);
        if ($query) {
            foreach ($query as $k => $v) {
                if (isset($gcats[$v['cid']])) {
                    $v['gcname'] = $gcats[$v['cid']];
                } else {
                    $v['gcname'] = '未分类';
                }
                $v['addtime'] = _date($v['addtime']);
                $list[] = $v;
            }
            $data['list'] = $list;
            $total = $this->db->get_one("select count(*) as total from " . table('category_attr') . " where pid = 0");
            if ($page_size < $total['total']) {
                $data['page'] = $this->uqcms_page(array('page' => $page, 'page_size' => $page_size, 'total' => $total['total']));
            }
            $this->assign('data', $data);
        }
        $this->display();
    }
    public function edit()
    {
        $id = !empty($_GET['id']) ? $_GET['id'] : false;
        if ($id) {
            $row = $this->module('category_attr')->get_one($id);
            $this->assign('row', $row);
        }
        $this->display('category_attr/edit');
    }
    public function save()
    {
        $data['cid'] = P('cid', '', '0');
        $data['pid'] = '0';
        $name = P('name', '名称不能为空');
        $data['px'] = P('px', '', '0');
        $data['addtime'] = time();
        $id = isset($_POST['id']) ? $_POST['id'] : false;
        if ($id) {
            $data['name'] = $name;
            $query = $this->db->update(table('category_attr'), $data, 'id = ' . $id);
        } else {
            $name_arr = explode(',', $name);
            $count = 0;
            $loop = 0;
            $this->db->autocommit();
            foreach ($name_arr as $v) {
                $data['name'] = $v;
                $exec = $this->db->add(table('category_attr'), $data);
                if ($exec) {
                    $count++;
                }
                $loop++;
            }
            if ($count == $loop && $count > '0') {
                $this->db->commit();
                $query = true;
            } else {
                $this->db->rollback();
                $query = false;
            }
        }
        if ($query) {
            right_json();
        } else {
            error_json('保存失败');
        }
    }
    public function child_index()
    {
        $pid = isset($_GET['pid']) ? $_GET['pid'] : showmsg('访问错误');
        $page = isset($_GET['page']) ? $_GET['page'] : '1';
        $page_size = isset($_GET['page_size']) ? $_GET['page_size'] : '20';
        $query = $this->db->get_all("select * from " . table('category_attr') . " where pid = " . $pid . " order by id desc limit " . ($page - 1) * $page_size . "," . $page_size);
        if ($query) {
            foreach ($query as $v) {
                $v['addtime'] = _date($v['addtime']);
                $list[] = $v;
            }
            $data['list'] = $list;
            $this->assign('data', $data);
        }
        $this->display();
    }
    public function child_edit()
    {
        $id = !empty($_GET['id']) ? $_GET['id'] : false;
        if ($id) {
            $row = $this->module('category_attr')->get_one($id);
            $this->assign('row', $row);
        }
        $this->display();
    }
    public function child_save()
    {
        $data['pid'] = P('pid', '提交PID错误');
        $data['px'] = P('px', '', '0');
        $data['addtime'] = time();
        $name = P('name', '名称不能为空');
        $id = isset($_POST['id']) ? $_POST['id'] : false;
        if ($id) {
            $data['name'] = $name;
            $query = $this->db->update(table('category_attr'), $data, 'id = ' . $id);
        } else {
            $name_arr = explode(',', $name);
            $count = 0;
            $loop = 0;
            $this->db->autocommit();
            foreach ($name_arr as $v) {
                $data['name'] = $v;
                $query = $this->db->add(table('category_attr'), $data);
                if ($query) {
                    $count++;
                }
                $loop++;
            }
            if ($count == $loop && $count > '0') {
                $this->db->commit();
                $query = true;
            } else {
                $this->db->rollback();
                $query = false;
            }
        }
        if ($query) {
            right_json();
        } else {
            error_json('保存失败');
        }
    }
    public function del()
    {
        $id = P('id', 'CID参数错误');
        $row = $this->db->get_one("select id,pid from " . table('category_attr') . " where pid = " . $id . " limit 1");
        if ($row) {
            if ($row['id']) {
                error_json('该分类下还有数据');
            } else {
                $query = $this->db->delete(table('category_attr'), 'id = ' . $id);
                if ($query) {
                    right_json();
                } else {
                    error_json('删除失败');
                }
            }
        } else {
            error_json('操作失败');
        }
    }
    public function child_del()
    {
        $id = P('id', '参数错误');
        $query = $this->db->delete(table('category_attr'), 'id = ' . $id);
        if ($query) {
            right_json();
        } else {
            error_json('删除失败');
        }
    }
}